package com.wangyadong.hobby.schedule.jobtask.javasync;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2018/11/1412:57
 **/
public class RunnableExample implements Runnable {
    private final Object MUTEX = new Object();

    @Override
    public void run() {

        synchronized (MUTEX) {
            System.out.println(Thread.currentThread().getName() + " ==============");
            shotSleep(30);
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
//        for (int i = 0; i < 10; i++) {
//            RunnableExample runnableExample = new RunnableExample();
//            new Thread(runnableExample).start();
//        }

//        IntStream.range(0, 10).forEach(i -> {
//            System.out.println("...........");
//            new Thread(RunnableExample::new).start();
//        });
        Class  clazz = Class.forName("com.wangyadong.hobby.service.CommonFactory",true,Thread.currentThread().getContextClassLoader());
        System.out.println("");

        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        String name = runtime.getName(); // format: "pid@hostname"
        Integer  PID = Integer.parseInt(name.substring(0, name.indexOf('@')));
        System.out.println("---"+PID);
    }

    private static void shotSleep(int seconds) {
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
